// packageE/healthy/healthyResult/healthyResult.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    questionFlag: false,
    posterFlag: false,
    recommend_tips: "",
    resultData: {},
    poster_url: "",
    member_id: "",
    health_id: 0,
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    let member_id = 0;
    if (!options.member_id || options.member_id == "") {
      try {
        member_id = wx.getStorageSync("yz_uid");
      } catch (error) {
        console.log(error);
      }
    } else member_id = options.member_id;

    this.data.member_id = member_id;

    let scene = decodeURIComponent(options.scene);
    if (scene) {
      var info_arr = [];
      info_arr = scene.split(",");
      for (let i = 0; i < info_arr.length; i++) {
        let chil_arr = [];
        chil_arr = info_arr[i].split("=");
        console.log(chil_arr);
        if (chil_arr[0] == "member_id") {
          this.data.member_id = chil_arr[1];
        }
      }
      console.log(info_arr);
    }

    this.getResultData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path:
        "/packageE/healthy/healthyResult/healthyResult?member_id=" +
        this.data.member_id,
    };
  },
  getResultData() {
    let urlStr = app.getNetAddresss(
      "plugin.health-assessment.api.index.getData"
    );
    let member_id = this.data.member_id;
    console.log(member_id);
    app._postNetWork({
      url: urlStr,
      data: {
        member_id,
        art: "getResult",
      },
      success: (resdata) => {
        let res = resdata.data;

        if (res.result != 1) return;
        let resultData = res.data.getResult;
        console.log("成功", resultData);
        this.getPoste(resultData.record_id, resultData.health_id);
        if (resultData.is_healthy == 1) {
          this.setData({
            health_id: resultData.health_id,
            recommend_tips: resultData.recommend_tips,
            questionFlag: true,
          });
        }
        this.setData({ resultData });
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  setQuestionFlag() {
    this.setData({
      questionFlag: !this.data.questionFlag,
    });
  },
  setPosterFlag() {
    this.setData({
      posterFlag: !this.data.posterFlag,
    });
  },
  resetHealthy() {
    let health_id = this.data.health_id;
    if (health_id < 1) return;
    wx.redirectTo({
      url: "/packageE/healthy/healthyPaper/healthyPaper?health_id=" + health_id,
    });
  },
  getPoste(record_id, health_id) {
    let urlStr = app.getNetAddresss(
      "plugin.health-assessment.api.index.getData"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        art: "getPoster",
        health_type: "2",
        record_id,
        health_id,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) {
          return;
        }

        this.setData({ poster_url: res.data.getPoster.url_path });
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  async checkWritePotosAlbum() {
    var _this = this;
    // var times = _this.data.timeend - _this.data.timestart;
    let writePhotosAlbum = await this.getAuthSetting("scope.writePhotosAlbum");
    if (!writePhotosAlbum) {
      wx.showModal({
        title: "请开启授权",
        content: "是否跳转设置页开启授权",
        success(res) {
          if (res.confirm) {
            wx.openSetting({
              success(settingdata) {
                if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                  _this.saveImg();
                } else {
                  console.log("还是没授权");
                }
              },
            });
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        },
      });
      return;
    }
    this.saveImg();
  },
  saveImg() {
    console.log("执行");
    var imgSrc = this.data.poster_url;
    wx.downloadFile({
      url: imgSrc,
      success: (res) => {
        console.log(res);
        //图片保存到本地
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: (data) => {
            wx.showToast({
              title: "保存成功",
              icon: "none",
              duration: 2000,
            });
            this.setPosterFlag();
          },
          fail: function (err) {
            console.log(err);
          },
        });
      },
    });
  },
  getAuthSetting(authName) {
    return new Promise((resolve, reject) => {
      wx.getSetting({
        success(res) {
          if (!res.authSetting[authName]) {
            wx.authorize({
              scope: authName,
              success() {
                resolve(true);
              },
              fail() {
                resolve(false);
              },
            });
          } else resolve(true);
        },
        fail() {
          resolve(false);
        },
      });
    });
  },
});
